//试算平衡表排序
export function trialNameSort(data, sortArr) {
    // let arr = [
    //   "2021_试算平衡-现金补充资料表_本期",
    //   "2021_试算平衡-现金流量表_上期",
    //   "2021_试算平衡-利润表_上期",
    //   "2017_试算平衡-现金补充资料表_本期",
    //   "2017_试算平衡-现金流量表_上期",
    //   "2019_试算平衡-利润表_上期",
    //   "2019_试算平衡-现金补充资料表_本期",
    //   "2020_试算平衡-现金流量表_上期",
    //   "2020_试算平衡-利润表_上期"
    // ];
    if (!data || !data.length) {
        return;
    }
    let arr = [];
    for (let i = 0; i < data.length; i++) {
        arr.push(data[i].name);
    }
    //默认排序
    const trialDefaultSort = [
        {
            title: "资产表_期末",
            number: 1,
        },
        {
            title: "资产表_期初",
            number: 2,
        },
        {
            title: "负债表_期末",
            number: 3,
        },
        {
            title: "负债表_期初",
            number: 4,
        },
        {
            title: "利润表_本期",
            number: 5,
        },
        {
            title: "利润表_上期",
            number: 6,
        },
        {
            title: "现金流量表_本期",
            number: 7,
        },
        {
            title: "现金流量表_上期",
            number: 8,
        },
        {
            title: "现金补充资料表_本期",
            number: 9,
        },
        {
            title: "现金补充资料表_上期",
            number: 10,
        },
        {
            title: "所有者权益变动表_本年",
            number: 11,
        },
        {
            title: "所有者权益变动表_上年",
            number: 12,
        },
    ];
    const defaultSort = sortArr ? sortArr : trialDefaultSort;
    // 构建一个映射，将报表标题映射到 tDefaultSort 中的 number
    let titleToNumberMap = defaultSort.reduce((acc, item) => {
        acc[item.title] = item.number;
        return acc;
    }, {});
    // 然后根据排序后的数组进行分组
    const groupedByYear = arr.reduce((groups, item) => {
        const year = item.substring(0, 4);
        if (!groups[year]) {
            groups[year] = [];
        }
        const title = item.split("-")[1];
        const currentData = data.filter((c) => c.name === item);
        groups[year].push({
            name: item,
            title,
            tid: currentData && currentData.length ? currentData[0].tid : "",
        });
        return groups;
    }, {});
    // 根据 tDefaultSort 中的 number 对 arr 进行排序
    let newGroupedByYear = {};
    for (let year in groupedByYear) {
        const categoryData = groupedByYear[year];
        categoryData.sort((a, b) => {
            let titleA = a.title;
            let titleB = b.title;
            let numberA = titleToNumberMap[titleA] || 999;
            let numberB = titleToNumberMap[titleB] || 999;
            return numberA - numberB;
        });
        newGroupedByYear[year] = categoryData;
    }
    // 获取对象的键并进行降序排序
    const sortedKeys = Object.keys(newGroupedByYear).sort((a, b) => parseInt(b) - parseInt(a));
    let result = [];
    for (let i = 0; i < sortedKeys.length; i++) {
        const key = sortedKeys[i];
        result.push(...groupedByYear[key]);
    }
    return result;
}
export function unFinancialNameSort(data, sortArr) {
    if (!data || !data.length) {
        return;
    }
    //默认排序
    const unDefaultSort = [
        {
            title: "资产表",
            number: 1,
        },
        {
            title: "负债表",
            number: 2,
        },
        {
            title: "利润表",
            number: 3,
        },
        {
            title: "现金流量表",
            number: 4,
        },
        {
            title: "现金流补充资料表",
            number: 5,
        },
    ];
    const defaultSort = sortArr ? sortArr : unDefaultSort;
    // 构建一个映射，将报表标题映射到 tDefaultSort 中的 number
    let titleToNumberMap = defaultSort.reduce((acc, item) => {
        acc[item.title] = item.number;
        return acc;
    }, {});
    data.sort((a, b) => {
        let titleA = a.name.split("-")[1];
        let titleB = b.name.split("-")[1];
        let numberA = titleToNumberMap[titleA];
        let numberB = titleToNumberMap[titleB];
        return numberA - numberB;
    });
    return data;
}
